<template>
  <el-dialog
    :title="view === 'create'? '添加规格信息': '修改规格信息'"
    :visible="true"
    custom-class="dialogWrapper"
    :before-close="closeDialog"
    >
    <el-form :model="formData" label-width="80px" ref="ruleForm" :rules="rules">
     <el-form-item label="电子券名称" prop="specName" required>
        <el-input v-model.trim="formData.specName" auto-complete="off"></el-input>
     </el-form-item>
     <el-form-item label="类型" prop="specName" required>
        <el-select v-model.trim="formData.parentId" placeholder="请选择电子券类型">
              <el-option 
              	v-for="item in SelectList"
      					:key="item.id"
      					:label="item.specName"
      					:value="item.id"
      				></el-option>
        </el-select>
     </el-form-item>
     <el-form-item label="发券途径" prop="specName" required>
         <el-select v-model.trim="formData.parentId" placeholder="请选择发券途径">
              <el-option 
              	v-for="item in SelectList"
      					:key="item.id"
      					:label="item.specName"
      					:value="item.id"
      				></el-option>
        </el-select>
     </el-form-item>
     <el-form-item label="电子券使用状态" prop="specName" required>
        <el-select v-model.trim="formData.parentId" placeholder="请选择电子券使用状态">
              <el-option 
              	v-for="item in SelectList"
      					:key="item.id"
      					:label="item.specName"
      					:value="item.id"
      				></el-option>
        </el-select>
     </el-form-item>
     <el-form-item label="面值" prop="specName" required>
        <el-input v-model.trim="formData.specName" auto-complete="off"></el-input>
     </el-form-item>
     <el-form-item label="总数量" prop="specName" required>
        <el-input v-model.trim="formData.specName" auto-complete="off"></el-input>
     </el-form-item>
     <el-form-item label="适用范围" prop="specName" required>
        <el-select v-model.trim="formData.parentId" placeholder="请选择适用范围">
              <el-option 
              	v-for="item in SelectList"
      					:key="item.id"
      					:label="item.specName"
      					:value="item.id"
      				></el-option>
        </el-select>
     </el-form-item>
     <el-form-item label="总数量" prop="specName" required>
        <el-input v-model.trim="formData.specName" auto-complete="off"></el-input>
     </el-form-item>
     <el-form-item label="有效开始时间">
         <div class="block">
				    <el-date-picker
				      v-model="formData.specName"
				      format="yyyy-MM-dd HH:mm:ss"
				      value-format="yyyy-MM-dd HH:mm:ss"
				      type="datetime"
				      :picker-options="pickerBeginDateBefore"
				      placeholder="选择日期时间">
				    </el-date-picker>
				  </div>
      </el-form-item>
      <el-form-item label="有效期结束时间">
         <div class="block">
				    <el-date-picker
				      v-model="formData.specName"
				      format="yyyy-MM-dd HH:mm:ss"
				      value-format="yyyy-MM-dd HH:mm:ss"
				      type="datetime"
				      :picker-options="pickerBeginDateAfter"
				      placeholder="选择日期时间">
				    </el-date-picker>
				  </div>
      </el-form-item>
     <!-- <el-form-item label="上级规格" required>
        <el-select v-model.trim="formData.parentId" placeholder="不选择即创建一级标签">
              <el-option 
              	v-for="item in SelectList"
      					:key="item.id"
      					:label="item.specName"
      					:value="item.id"
      				></el-option>
        </el-select>
     </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submit('ruleForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { create, edit,select } from "@/services/spec";
import { RequestError } from "@/services/errors";
import { mapActions } from "vuex";
// import {login} from '@/services/login'
// import menuModules from '@/router/modules.json'
var vailNumber = (rule, value, callback) => {
	var NumberyRegex = /^[0-9]*$/;
	if (!NumberyRegex.test(value)) {
	    callback(new Error('格式不正确,只能输入数字'))
	    } else {
	        callback();
	    }
};
export default {
  name: "edit-role",
  data () {
    return {
      id:"",
      SelectList:[],
      formData: {
        specName: "",
        parentId: ""
      },
      dialogFormVisible: false,
      pickerBeginDateBefore:{
            disabledDate: (time) => {
                let beginDateVal = this.formData.endDate;
                if (beginDateVal) {
                    return time.getTime() > beginDateVal;
                }
            }
			},
			pickerBeginDateAfter:{
					disabledDate: (time) => {
							let beginDateVal = this.formData.startDate;
							if (beginDateVal) {
									return time.getTime() < beginDateVal;
							}
					}
			},
      rules: {
        specName: [{ required: true, message: "请输入规格名称", trigger: "blur" },
        { min: 1, max: 50, message: '长度在 50个字符以内', trigger: 'blur' }]
      }
    }
  },
  props: {
    view: {
      type: String,
      default: "create"
    }
  },
  methods: {
    ...mapActions(["setUserInfo"]),
    closeDialog() {
      this.$emit("close");
    },
    async loadSelect () {
        try {
          let data = await select()
          this.SelectList = data.specCombo
          if(this.formData.parentId == "0"){
            this.formData.parentId = ""
          }
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
    },
    submit(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          try {
            if (this.view === "create") {
              if(this.formData.parentId == ""){
                  await create({
                    specName: this.formData.specName,
                    parentId: "0",
                  });
              }else{
                  await create({
                    specName: this.formData.specName,
                    parentId: this.formData.parentId,
                  });
              }
            } else {
              if(this.formData.parentId == ""){
                await edit({
                  id: this.id,
                  specName: this.formData.specName,
                  parentId: "0"
                });
              }else{
                await edit({
                  id: this.id,
                  specName: this.formData.specName,
                  parentId: this.formData.parentId
                });
              }
              
            }
            this.dialogFormVisible = false;
            this.$emit("close", true);
            this.$message({
              showClose: true,
              message: "提交成功",
              type: "success"
            });
          } catch (e) {
            // this.$alert(e.message, "提示", {
            //   type: "error",
            //   confirmButtonText: "确定"
            // });
            if (e instanceof RequestError) {
              this.$alert(e.message, "提示", {
                type: "error",
                confirmButtonText: "确定"
              });
            } else {
              throw e;
            }
          }
        } else {
          return;
        }
      });
    }
  },
  mounted() {
        
  }
};
</script>
<style>
.dialogWrapper {
  width: 540px;
}
.el-select {
  width: 100%;
}
</style>
